package scm.erp.sale.plan.controller;
import com.github.pagehelper.PageInfo;
import jakarta.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import scm.erp.core.domain.Pg;
import scm.erp.core.domain.Ret;
import scm.erp.sale.plan.domain.Plan;
import scm.erp.sale.plan.service.PlanService;
@Slf4j
@RestController
@RequestMapping("/plan")
public class PlanController {
    private final PlanService service;

    public PlanController(PlanService service) {

        this.service = service;
    }

    /**
     * 查询单条（未用）
     *
     * @param id 数据ID
     * @return PlanVo 查询结果
     */
    @GetMapping("/one/{id}")
    public Ret<Plan.Vo> one(@PathVariable String id) {
        Plan.Vo PlanVo = service.one(id);
        return Ret.ok(PlanVo);
    }
    /**
     * 新增
     *
     * @param vo 新增数据
     * @return 数据ID
     */
    @PostMapping("/insert")
    public Ret<String> insert(
            @Validated @RequestBody Plan.Vo vo) {
        String id = service.insert(vo);
        return Ret.ok(id);
    }

    /**
     * 更新
     *
     * @param vo 更新数据
     * @return 更新结果
     */
    @PostMapping("/update")
    public Ret<Boolean> update(
            @Validated @RequestBody Plan.Vo vo) {
        Boolean ret = service.update(vo);
        return Ret.ok(ret);
    }

    @PostMapping("/page")
    public Ret<Object> page(
            @Validated @RequestBody Pg<Plan.Entity, Plan.SearchVo> pg) {
        PageInfo<Plan.Entity> pageInfo = pg
                .startPage()
                .selectPage(() -> this.service.list(pg.getPageData()))
                .closePage()
                .getPageInfo();
        return Ret.ok(pageInfo);
    }
    /**
     * 启用
     *
     * @param id
     * @return
     */
    @PostMapping("/enable/{id}")
    public Ret<Integer> enable(
            @PathVariable @NotEmpty(message = "id不能为空！") String id) {
        int i = this.service.updateStatusEnable(id);
        return Ret.ok(i);
    }

    /**
     * 禁用
     *
     * @param id
     * @return
     */
    @PostMapping("/disable/{id}")
    public Ret<Integer> disable(
            @PathVariable @NotEmpty(message = "id不能为空！") String id) {
        int i = this.service.updateStatusDisable(id);
        return Ret.ok(i);
    }
}
